﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.HtmlControls;

namespace ReconUI
{
    public partial class SearchUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                control.Value = Request.QueryString["textbox"].ToString();
                path.Value = Request.QueryString["PageRequest"].ToString();
                BindGrid();
                txtUserId.Focus();
               
            }
            //try
            //{
            //    //Get connection string from Web.Config '127.0.0.1:42424
            //    if (!string.IsNullOrEmpty(Session["UserSearch"].ToString()))
            //    {
            //        txtUserId.Text = Session["UserSearch"].ToString();
            //        Session["UserSearch"] = null;
            //    }

            //    //userId.Value = Request.QueryString("user").ToString()

            //    if (!IsPostBack)
            //    {
            //        txtUserId.Focus();
            //        bindgrid();
            //    }
            //}
            //catch (Exception ex)
            //{

            //}
        }

        #region User Defined Function

        public void BindGrid()
        {
            DataTable dtUserDetails = new DataTable();
            DBFactory objDBfactory = new DBFactory();

            try
            {

                if (txtUserId.Text.Trim() == "")
                {
                    dtUserDetails = objDBfactory.GetUserDetailsByUserId(Convert.ToString(control.Value).Trim());

                    if ((dtUserDetails) == null)
                    {
                        lblusercnt.Text = "No Records Found";
                    }
                    else if (dtUserDetails.Rows.Count >= 1)
                    {
                        gvUser.DataSource = dtUserDetails;
                        gvUser.DataBind();
                        lblusercnt.Text = ((DataTable)gvUser.DataSource).Rows.Count.ToString() + " records found.";
                    }
                    else
                    {
                        lblusercnt.Text = "No Records Found";
                    }
                }
                else
                {
                    dtUserDetails = objDBfactory.GetUserDetailsByUserId(Convert.ToString(txtUserId.Text).Trim());

                    if ((dtUserDetails) == null)
                    {
                        lblusercnt.Text = "No Records Found";
                    }
                    else if (dtUserDetails.Rows.Count >= 1)
                    {
                        gvUser.DataSource = dtUserDetails;
                        gvUser.DataBind();
                        lblusercnt.Text = ((DataTable)gvUser.DataSource).Rows.Count.ToString() + " records found.";
                    }
                    else
                    {
                        lblusercnt.Text = "No Records Found";
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if ((dtUserDetails) != null)
                {
                    dtUserDetails.Dispose();
                }
                dtUserDetails = null;
            }
        }

        #endregion
        #region Gridview Events
        //protected void gvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    gvUser.PageIndex = e.NewPageIndex;
        //    BindGrid();
        //}
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvUser.PageIndex = 0;
            gvUser.DataSource = null;
            gvUser.DataBind();
            BindGrid();
        }
        protected void gvUser_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {

        }

        protected void gvUser_SelectedIndexChanged(object sender, EventArgs e)
        {
            string userid;
            userid = ((LinkButton)gvUser.SelectedRow.FindControl("lbtnuserid")).Text;
            string strScript = "<script>";
            strScript += "window.opener.document.all('ctl00$BodyHolder$txtUserId').value = '" + userid + "';";
            strScript += "self.close();</script>";
            Response.Write(strScript);
        }

        #endregion
    }
}